home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 3 / Amiga Tools 3.iso / grafik / raytracing / rayshade-4.0.6.3 / libray / libtext / textaux.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-09  |  5.7 KB  |  300 lines

  1. /*
  2.  * textaux.c
  3.  *
  4.  * Copyright (C) 1989, 1991, Craig E. Kolb, Rod G. Bogart, Robert F. Skinner
  5.  * All rights reserved.
  6.  *
  7.  * This software may be freely copied, modified, and redistributed
  8.  * provided that this copyright notice is preserved on all copies.
  9.  *
  10.  * You may not distribute this software, in whole or in part, as part of
  11.  * any commercial product without the express consent of the authors.
  12.  *
  13.  * There is no warranty or other guarantee of fitness of this software
  14.  * for any purpose.  It is provided solely "as is".
  15.  *
  16.  * textaux.c,v 4.1 1994/08/09 08:03:24 explorer Exp
  17.  *
  18.  * textaux.c,v
  19.  * Revision 4.1  1994/08/09  08:03:24  explorer
  20.  * Bump version to 4.1
  21.  *
  22.  * Revision 1.1.1.1  1994/08/08  04:52:15  explorer
  23.  * Initial import.  This is a prerelease of 4.0.6enh3, or 4.1 possibly.
  24.  *
  25.  * Revision 4.0  91/07/17  14:44:05  kolb
  26.  * Initial version.
  27.  * 
  28.  */
  29. #include "texture.h"
  30.  
  31. Color *
  32. ColormapRead(filename)
  33. char *filename;
  34. {
  35.     FILE *fp;
  36.     Color *map;
  37.     char buf[BUFSIZ];
  38.     int r, g, b, i, num;
  39.  
  40.     fp = fopen(filename, "r");
  41.     if (fp == (FILE *)NULL) {
  42.         RLerror(RL_ABORT, "Cannot open colormap file \"%s\".\n",
  43.                 filename);
  44.         return (Color *)NULL;
  45.     }
  46.  
  47.     map = (Color *)Calloc(256, sizeof(Color));
  48.  
  49.     for (i = 0; fgets(buf,BUFSIZ,fp) != NULL && i < 256; i++) {
  50.         num = sscanf(buf,"%d %d %d", &r, &g, &b);
  51.         if (num != 3) {
  52.             RLerror(RL_ABORT,
  53.                 "%s, line %d:  Bad color specification.\n",
  54.                 filename, i+1);
  55.             return (Color *)NULL;
  56.         }
  57.         map[i].r = (Float)r;
  58.         map[i].g = (Float)g;
  59.         map[i].b = (Float)b;
  60.         ColorScale(1. / 255., map[i], &map[i]);
  61.     }
  62.     (void)fclose(fp);
  63.     return map;
  64. }
  65.  
  66. Float
  67. Marble(vec)
  68. Vector *vec;
  69. {
  70.     Float i;
  71.  
  72.     i = sin(8. * Chaos(vec, 6) + 7. * vec->z) + 1;
  73.     
  74.     return pow(0.5 * i, 0.77);
  75. }
  76.  
  77. Float
  78. PAChaos(vec, octaves)
  79. Vector *vec;
  80. int octaves;
  81. {
  82.     Float s, t, tmp;
  83.     Vector tp;
  84.  
  85.     s = 1.0;
  86.     t = 0.;
  87.     tp = *vec;
  88.  
  89.     while (octaves--) {
  90.         tmp = Noise3(&tp) * s;
  91.         t += fabs(tmp);
  92.         s *= 0.5;
  93.         tp.x *= 2.;
  94.         tp.y *= 2.;
  95.         tp.z *= 2.;
  96.     }
  97.  
  98.     return t;
  99. }
  100.  
  101. Float
  102. Chaos(vec, octaves)
  103. Vector *vec;
  104. int octaves;
  105. {
  106.     Float s, t;
  107.     Vector tp;
  108.  
  109.     s = 1.0;
  110.     t = 0.;
  111.     tp = *vec;
  112.  
  113.     while (octaves--) {
  114.         t += Noise3(&tp) * s;
  115.         s *= 0.5;
  116.         tp.x *= 2.;
  117.         tp.y *= 2.;
  118.         tp.z *= 2.;
  119.     }
  120.  
  121.     return t;
  122. }
  123.  
  124. void
  125. VfBm(vec, omega, lambda, octaves, ans)
  126. Vector *vec, *ans;
  127. Float omega, lambda;
  128. int octaves;
  129. {
  130.     Float o;
  131.     Vector tp, n;
  132.  
  133.     ans->x = ans->y = ans->z = 0.;
  134.     tp = *vec;
  135.     o = 1.;
  136.  
  137.     while (octaves--) {
  138.         DNoise3(&tp, &n);
  139.         ans->x += o * n.x;
  140.         ans->y += o * n.y;
  141.         ans->z += o * n.z;
  142.         o *= omega;
  143.         if (o < EPSILON)
  144.             break;
  145.         tp.x *= lambda;
  146.         tp.y *= lambda;
  147.         tp.z *= lambda;
  148.     }
  149. }
  150.  
  151. Float
  152. fBm(vec, omega, lambda, octaves)
  153. register Vector *vec;
  154. Float omega, lambda;
  155. int octaves;
  156. {
  157.     Float a, o;
  158.     Vector tp;
  159.  
  160.     a = 0; o = 1.;
  161.     tp = *vec;
  162.     while (octaves--) {
  163.         a += o * Noise3(&tp);
  164.         tp.x *= lambda;
  165.         tp.y *= lambda;
  166.         tp.z *= lambda;
  167.         o *= omega;
  168.     }
  169.     return a;
  170. }
  171.  
  172. int
  173. TileValue(tileu, tilev, u, v)
  174. Float tileu, tilev, u, v;
  175. {
  176.     /*
  177.      * If both tileu and tilev are zero, the pattern is repeated infinitly.
  178.      *   XXXXXX
  179.      *   XXXXXX   tileu=0  tilev=0
  180.      *   XXXXXX
  181.      *   XXXXXX
  182.      * If one is positive and the other is zero, the pattern is infinite
  183.      * in one direction and limited in the other.
  184.      *   ++++++
  185.      *   XXXXXX   tileu=0  tilev=1
  186.      *   ++++++
  187.      *   ++++++
  188.      * If both are positive, the pattern is limited in both directions.
  189.      *   ++++++
  190.      *   +++XX+   tileu=2  tilev=1
  191.      *   ++++++
  192.      *   ++++++
  193.      * If one is negative and the other is zero, the pattern is the
  194.      * inverse of the positive/zero case.
  195.      *   XXXXXX
  196.      *   ++++++   tileu=0  tilev=-1
  197.      *   XXXXXX
  198.      *   XXXXXX
  199.      * If one is negative and the other is positive, the pattern is like
  200.      * negative/zero, but limited in one direction.
  201.      *   +++XX+
  202.      *   ++++++   tileu=2  tilev=-1
  203.      *   +++XX+
  204.      *   +++XX+
  205.      * If both are negative, the pattern is the inverse of the 
  206.      * positive/positive case (a rectangular hole).
  207.      *   XXXXXX
  208.      *   XXX++X   tileu=-2  tilev=-1
  209.      *   XXXXXX
  210.      *   XXXXXX
  211.      */
  212.     if ((tileu < 0.001) && (tileu > -0.001) && 
  213.         (tilev < 0.001) && (tilev > -0.001))
  214.         /* zero/zero */
  215.         return FALSE;
  216.     if ((tileu < -0.001) && (tilev < -0.001) &&
  217.         ((u > -tileu) || (u < 0) || (v > -tilev) || (v < 0)))
  218.         /* negative/negative */
  219.         return FALSE;
  220.     if ((tileu > 0.001) && ((u > tileu) || (u < 0)))
  221.         /* positive/whatever */
  222.         return TRUE;
  223.     if ((tilev > 0.001) && ((v > tilev) || (v < 0)))
  224.         /* whatever/positive */
  225.         return TRUE;
  226.     if ((tileu < -0.001) && (u < -tileu) && (u > 0))
  227.         /* negative/whatever */
  228.         return TRUE;
  229.     if ((tilev < -0.001) && (v < -tilev) && (v > 0))
  230.         /* whatever/negative */
  231.         return TRUE;
  232.  
  233.     return FALSE;
  234. }
  235.  
  236. void
  237. Wrinkled(pos, lambda, omega, octaves, res)
  238. Vector *pos, *res;
  239. Float lambda, omega;
  240. int octaves;
  241. {
  242.     Float s;
  243.     Vector point, tmp;
  244.  
  245.     res->x = res->y = res->z = 0.;
  246.     s = 1.;
  247.     point = *pos;
  248.     while (octaves--) {
  249.         DNoise3(&point, &tmp);
  250.         point.x *= lambda;
  251.         point.y *= lambda;
  252.         point.z *= lambda;
  253.         res->x += tmp.x * s;
  254.         res->y += tmp.y * s;
  255.         res->z += tmp.z * s;
  256.         s *= omega;
  257.     }
  258. }
  259.  
  260. void
  261. Windy(pos,windscale,chaoscale,bumpscale,octaves,tscale,hscale,offset,res)
  262. Vector *pos, *res;
  263. Float windscale, chaoscale, bumpscale, tscale, hscale, offset;
  264. int octaves;
  265. {
  266.     Vector spoint, tmp;
  267.     Float windfield, f, scalar;
  268.  
  269.     spoint = *pos;
  270.     spoint.x *= windscale;
  271.     spoint.y *= windscale;
  272.     spoint.z *= windscale;
  273.     if (chaoscale)
  274.         windfield = chaoscale * Chaos(&spoint, 7);
  275.     else
  276.         windfield = 1.;
  277.  
  278.     DNoise3(pos, &tmp);
  279.     res->x = bumpscale * tmp.x;
  280.     res->y = bumpscale * tmp.y;
  281.     res->z = bumpscale * tmp.z;
  282.  
  283.     f = 1.;
  284.     scalar = windfield;
  285.     while (octaves--) {
  286.         f *= tscale;
  287.         spoint.x = f*pos->x;
  288.         spoint.y = f*pos->y;
  289.         spoint.z = f*pos->z;
  290.         DNoise3(&spoint, &tmp);
  291.         res->x += scalar*tmp.x;
  292.         res->y += scalar*tmp.y;
  293.         res->z += scalar*tmp.z;
  294.         scalar *= hscale;
  295.     }
  296.     res->x *= windfield + offset;
  297.     res->y *= windfield + offset;
  298.     res->z *= windfield + offset;
  299. }
  300.